package org.example;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.TimeUnit;


@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Thread)
@Fork(1)
@Warmup(iterations = 3)
@Measurement(iterations = 10)
public class JMHMain {

    private static LinkNode HEAD = null;

    @Benchmark
    public void testSearchLinkData() {
        int nodeNum = 500000;
        initLinkNode(nodeNum);
        searchLinkData(HEAD, nodeNum);
    }

    public void initLinkNode(int nodeNum) {
        if (HEAD != null) {
            return;
        }
        HEAD = new LinkNode();
        LinkNode cursorLinkNode = HEAD;

        for (int i = 1; i <= nodeNum; i++) {
            LinkNode tmpNode = new LinkNode();
            tmpNode.data = i;
            tmpNode.next = null;
            cursorLinkNode.next = tmpNode;
            cursorLinkNode = tmpNode;
        }
    }

    public void searchLinkData(LinkNode head, int destData) {
        LinkNode cursorLinkNode = head.next;
        int i = 0;
        while (cursorLinkNode != null) {
            i++;
            if (cursorLinkNode.data == destData) {
                System.out.printf("第 %d 次，寻找到了目标值：:%d\n", i, destData);
            }
            cursorLinkNode = cursorLinkNode.next;
        }
    }

    public static void main(String[] args) throws Exception {
        Options opt = new OptionsBuilder()
                .include(JMHMain.class.getSimpleName())
                .build();
        new Runner(opt).run();
    }
}